﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace EAS.Content.Projectiles
{
    public class WaterProjectile : ModProjectile
    {
        public override void SetDefaults()
        {
            Projectile.width = 16;
            Projectile.height = 16;
            Projectile.friendly = true;
            Projectile.penetrate = 1;
            Projectile.timeLeft = 300;
            Projectile.tileCollide = true;
            Projectile.ignoreWater = true;
        }
        public override void AI()
        {
            
            Vector2 targetPos = Projectile.Center + Projectile.velocity;
            float distance = Vector2.Distance(Projectile.Center, targetPos);
            Projectile.velocity = Vector2.Lerp(Projectile.velocity, Vector2.Normalize(targetPos - Projectile.Center) * 8f, 0.1f);

            for (int i = 0; i < 2; i++)
            {
                Dust dust = Dust.NewDustDirect(Projectile.position, Projectile.width, Projectile.height, DustID.Water, 0f, 0f, 100, default, 1.5f);
                dust.noGravity = true;
                dust.velocity *= 1.5f;
                dust.velocity += Projectile.velocity * 0.5f;
                dust.alpha = 100;
                dust.scale *= 1.2f;
            }           
        }

        public override bool PreDraw(ref Color lightColor)
        {
            Texture2D texture = ModContent.Request<Texture2D>(Texture).Value;
            Rectangle sourceRectangle = new Rectangle(0, 0, texture.Width, texture.Height);
            Vector2 origin = sourceRectangle.Size() / 2f;

            Main.EntitySpriteDraw(texture, Projectile.position - Main.screenPosition + origin, sourceRectangle, lightColor, Projectile.rotation, origin, Projectile.scale, SpriteEffects.None, 0);

            return false;
        }
    }
}
